package com.xueqiu.lovepetmedicalmanagersystem.service.Impl;

import com.xueqiu.lovepetmedicalmanagersystem.mapper.OwnersMapper;
import com.xueqiu.lovepetmedicalmanagersystem.mapper.PetTypesMapper;
import com.xueqiu.lovepetmedicalmanagersystem.mapper.PetsMapper;
import com.xueqiu.lovepetmedicalmanagersystem.pojo.Owners;
import com.xueqiu.lovepetmedicalmanagersystem.pojo.PetTypes;
import com.xueqiu.lovepetmedicalmanagersystem.pojo.Pets;
import com.xueqiu.lovepetmedicalmanagersystem.service.PetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xueqiu
 */
@Service
public class PetServiceImpl implements PetService {

    @Autowired
    private PetsMapper petsMapper;
    @Autowired
    private OwnersMapper ownersMapper;
    @Autowired
    private PetTypesMapper petTypesMapper;

    /**
     * 查询所有宠物
     * @return 宠物对象列表；可能找不到为EMPTY
     */
    @Override
    public List<Pets> queryAllPets() {
        return petsMapper.queryAllPets();
    }

    /**
     * 根据宠物姓名关键词模糊查询多个宠物
     * @param petName 宠物姓名关键词
     * @return 符合关键词的宠物对象列表；可能找不到为EMPTY
     */
    @Override
    public List<Pets> queryPetByName(String petName) {
        List<Pets> petsList = petsMapper.queryPetLikeName(petName);
        for (Pets pets : petsList) {
            Owners owners = ownersMapper.queryOwnerById(pets.getOwnerId());
            pets.setOwners(owners);
        }

        return petsList;
    }

    /**
     * 根据宠物主人ID查询多个宠物
     * @param ownId 宠物主人ID
     * @return 宠物对象列表；可能找不到为EMPTY
     */
    @Override
    public List<Pets> queryPetByOwnId(Integer ownId) {

        return petsMapper.queryPetByOwnId(ownId);
    }

    /**
     * 根据宠物姓名查询单个宠物；前提是宠物姓名无重复
     * @param petName 宠物姓名
     * @return 宠物对象；可能找不到为NULL
     */
    @Override
    public Pets getPetByName(String petName) {
        Pets pet = petsMapper.queryPetByName(petName);
        if (pet != null) {
            Owners owners = ownersMapper.queryOwnerById(pet.getOwnerId());
            PetTypes petType = petTypesMapper.queryPetType(pet.getTypeId());
            pet.setOwners(owners);
            pet.setTypeName(petType.getName());
        }

        return pet;
    }

    /**
     * 根据宠物ID更新单个数据
     * @param pets 数据源，包含宠物ID
     * @return 更新条数
     */
    @Override
    public Integer updatePet(Pets pets) {
        return petsMapper.updatePet(pets);
    }

    /**
     * 新增单个宠物数据
     * @param pets 数据源，不包含宠物ID
     * @return 新增条数
     */
    @Override
    public Integer insertPet(Pets pets) {
        return petsMapper.insertPet(pets);
    }
}
